package com.hsw.habit.service.impl;

import com.hsw.habit.mapper.TaskGroupsMapper;
import com.hsw.habit.mapper.TaskItemMapper;
import com.hsw.habit.model.TaskGroups;
import com.hsw.habit.model.TaskItem;
import com.hsw.habit.model.TaskItemExample;
import com.hsw.habit.model.bean.ListResponse;
import com.hsw.habit.service.GroupService;
import com.hsw.habit.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Resource
    private TaskItemMapper mapper;

    @Resource
    private GroupService groupService;

    private final static int PAGE_SIZE = 20;

    @Override
    public ListResponse<TaskItem> findTaskByParams(TaskItem taskItem) {
        TaskItemExample example = new TaskItemExample();
        TaskItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(taskItem.getStatus());
        criteria.andIsdeletedEqualTo((short) 0);
        example.setStartRow(taskItem.getStartRow());
        example.setPageSize(PAGE_SIZE);

        ListResponse<TaskItem> listResponse = new ListResponse<>();
        listResponse.setList(mapper.selectByExample(example));
        listResponse.setEnd(true);
        return listResponse;
    }

    /**
     * @param type
     * 0：全部
     * 1：未完成
     * 2：未开始
     * 3：已完成
     * @return
     */
    @Override
    public ListResponse<TaskItem> findTaskByType(Integer userId, Integer startRow, Integer type) {
        int currentTime = (int) (System.currentTimeMillis() / 1000);
        List<TaskGroups> groupsList = groupService.selectByUserId(userId);

        TaskItemExample example = new TaskItemExample();
        example.setPageSize(PAGE_SIZE);
        if (null == startRow) {
            startRow = 0;
        }
        example.setStartRow(startRow);
        example.setOrderByClause("id desc");

        TaskItemExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userId);
        criteria.andIsdeletedEqualTo((short) 0);
        switch (type) {
            case 1:
                criteria.andStatusNotEqualTo((short) 3);
                criteria.andStarttimeLessThan(currentTime);
                break;
            case 2:
                criteria.andStatusNotEqualTo((short) 3);
                criteria.andStarttimeGreaterThan(currentTime);
                break;
            case 3:
                criteria.andStatusEqualTo((short) 3);
                break;
            default:
                break;
        }

        ListResponse<TaskItem> response = new ListResponse<>();

        List<TaskItem> taskList = mapper.selectByExample(example);


        if (null != taskList && taskList.size() > 0) {
            for (TaskItem taskItem : taskList) {
                for (TaskGroups groups : groupsList) {
                    if (taskItem.getGroupId().equals(groups.getId())) {
                        taskItem.setGroupName(groups.getGroupName());
                        break;
                    }
                }
            }
        }

        response.setList(taskList);

        if (null != taskList && taskList.size() == PAGE_SIZE) {
            response.setEnd(false);
            response.setStartRow(startRow + PAGE_SIZE);
        } else {
            response.setEnd(true);
        }

        return response;
    }

    @Override
    public TaskItem findTaskItemById(Long id) {
        TaskItem taskItem = mapper.selectByPrimaryKey(id);
        if (null == taskItem || taskItem.getIsDeleted() == 1) {
            return null;
        }
        TaskGroups group = groupService.selectGroupById(taskItem.getGroupId());

        if (null != group) {
            taskItem.setGroupName(group.getGroupName());
        }
        return taskItem;
    }

    @Override
    public boolean updateById(TaskItem taskItem) {
        int res = mapper.updateByPrimaryKeySelective(taskItem);
        return res == 1;
    }

    @Override
    public boolean deleteById(Long id) {
        TaskItem taskItem = new TaskItem();
        taskItem.setId(id);
        taskItem.setIsDeleted((short) 1);
        int res = mapper.updateByPrimaryKeySelective(taskItem);
        return res == 1;
    }

    @Override
    public boolean addItemByParams(TaskItem taskItem) {
        int res = mapper.insertSelective(taskItem);
        return res == 1;
    }
}
